﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 页面辅助操作类
//最后修改: 2010-10-11
//最后修改: 2010-10-14 李晓忠 添加资源发布目录规则
//          2010-10-18 李城  针对李晓忠 写的代码  添加代码注释
using System;
using System.Collections.Generic;
using System.Text;
using TRPOP.Common;
using System.Web.UI.WebControls;
using System.Web;
using System.Data;

namespace TRPOP.Common
{
    /// <summary>
    /// 页面辅助类
    /// </summary>
    public class PageAssist
    {

        #region 页面通用的判断方法
        /// <summary>
        /// 根据传递参数返回 新增 修改
        /// </summary>
        /// <param name="isAdd"></param>
        /// <returns></returns>
        public static string AddOrEdit(bool isAdd)
        {
            return isAdd ? "新增" : "修改";
        }

        /// <summary>
        /// 检测 TextBox true 则通过 反之不通过
        /// </summary>
        /// <param name="txtBox"></param>
        /// <param name="msg">返回的string提示，必须初始化</param>
        /// <returns></returns>
        public static bool CheckForm(TextBox txtBox, ref string msg)
        {
            if (txtBox.Text.IsEmpty())
            {
                msg = msg + " 不能为空 ";
                return false;
            }
            else
                return true;
        }

        #endregion

        #region 获取 全部的站点类型 全部的文字替换类型  全部的字段类型  所有参数的数值类型 

        /// <summary>
        /// 获取全部的站点类型
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetAllPSNType(string strAdminRoot)
        {
            Dictionary<int, string> odnDictionary = new Dictionary<int, string>();

            string strPath = Util.GetMapPath("~/" + strAdminRoot + "/config/PSNType.config");
            
            DataTable table  =  ConfigManage.GetDataSource(strPath, "//items");
            if (table != null)
            {
                foreach (DataRow TempRow in table.Rows)
                {
                    odnDictionary.Add(TempRow["Value"].ToString().ToInt(), TempRow["Key"].ToString());
                }
            }
            else
            {
                odnDictionary.Add(0, "本地站点");
            }
            return odnDictionary;
        }

        
        /// <summary>
        /// 获取全部的文字替换类型
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetAllWordReplaceType(string strAdminRoot)
        {
            Dictionary<int, string> odnDictionary = new Dictionary<int, string>();

            string strPath = Util.GetMapPath("~/" + strAdminRoot + "/config/WordReplaceType.config");
            DataTable table = ConfigManage.GetDataSource(strPath, "//items");
            if (table != null)
            {
                foreach (DataRow TempRow in table.Rows)
                {
                    odnDictionary.Add(TempRow["Value"].ToString().ToInt(), TempRow["Key"].ToString());
                }
            }
            else
            {
                odnDictionary.Add(0, "简单文字替换");
                odnDictionary.Add(0, "高级文字替换");
            }
            return odnDictionary;
        }

 

        /// <summary>
        /// 获取全部的字段类型
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetAllFieldType(string strAdminRoot)
        {
            Dictionary<string, string> odnDictionary = new Dictionary<string, string>();

            string strPath = Util.GetMapPath("~/" + strAdminRoot + "/config/FieldType.config");
            DataTable table = ConfigManage.GetDataSource(strPath, "//items");
            if (table != null)
            {
                foreach (DataRow TempRow in table.Rows)
                {
                    odnDictionary.Add(TempRow["Value"].ToString(), TempRow["Key"].ToString());
                }
            }
            else
            {
                odnDictionary.Add("字符串(长度最大250)", "varchar");
                odnDictionary.Add("数值", "int");
                odnDictionary.Add("一般文本(不用设长度)", "text");
            }
            return odnDictionary;
        }
        /// <summary>
        /// 获取所有参数的数值类型
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetAllVarInputType()
        {
            Dictionary<string, string> odnDictionary = new Dictionary<string, string>();
            odnDictionary.Add("文本", "text");
            odnDictionary.Add("布尔值", "radio");
            odnDictionary.Add("文本域", "textarea");
            odnDictionary.Add("PSN站点", "PSN");
            odnDictionary.Add("PSNURL", "PSNURL");
            return odnDictionary;
        }

        /// <summary>
        /// 获取所有的表单值采集器类型
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GatAllFieldInputPicker()
        {
            Dictionary<string, string> odnDictionary = FieldInputPickerType.GetAll();  
            return odnDictionary;
        }

        #endregion

        #region 对控件的操作
        /// <summary>
        /// 让CheckBoxList多选
        /// </summary>
        /// <param name="values">value 字符串</param>
        /// <param name="interval">value的间隔</param>
        /// <param name="CHKL">控件</param>
        public static void  ControlCHKSelect(string values, string interval,  CheckBoxList CHKL)
        {
            
            StringBuilder ValuesArray = new StringBuilder();
            ValuesArray.Append("|");
            foreach (string TempValue in values.SplitString(interval))
            {
                ValuesArray.Append(TempValue + "|");
            }
            string ValuesStr = ValuesArray.ToString();
            foreach (ListItem item in CHKL.Items)
            {
                if (ValuesStr.IndexOf("|" + item.Value + "|") >= 0)
                {
                    item.Selected = true;
                }
            }
        }
        /// <summary>
        /// 获取CheckBoxList多选值
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="CHKL"></param>
        /// <returns></returns>
        public static string  ControlCHKGetValue(string interval,   CheckBoxList CHKL)
        {
            StringBuilder ValuesArray = new StringBuilder();
            bool IsNew = true;
            foreach (ListItem item in CHKL.Items)
            {
                if (IsNew && item.Selected)
                {
                    ValuesArray.Append(item.Value);
                    IsNew = false;
                }
                else if(item.Selected)
                {
                    ValuesArray.Append(interval + item.Value);
                }
            }
            return ValuesArray.ToString();
        }



        #endregion

        #region  生成指定数量的字符串
        /// <summary>
        /// 生成指定数量的字符串
        /// </summary>
        /// <param name="nCount"></param>
        /// <param name="AddStr"></param>
        /// <returns></returns>
        public static string AppendStr(int nCount, string AddStr)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < nCount; i++)
            {
                sb.Append(AddStr);
            }
            return sb.ToString();
        }
        #endregion

        #region 资源发布目录规则
        /// <summary>
        /// 资源发布目录规则
        /// </summary>
        public static Dictionary<string, string> SourceRootReg {
            get {
                Dictionary<string, string> dicSourceRootReg = new Dictionary<string, string>();
                dicSourceRootReg.Add("-", "");
                dicSourceRootReg.Add("none", "");
                dicSourceRootReg.Add("Y/md", "Y/md");
                dicSourceRootReg.Add("Y-m-d", "Y-m-d");
                dicSourceRootReg.Add("Y/m/d", "Y/m/d");
                dicSourceRootReg.Add("Y-m", "Y-m");
                dicSourceRootReg.Add("Y", "Y");
                dicSourceRootReg.Add("auto", "auto");
                return dicSourceRootReg;
            }
        }
        #endregion

        /// <summary>
        /// 将c#时间转换成UNIX时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long ConvertDateTimeInt(DateTime time)
        {
            long intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));

            intResult = (long)(time - startTime).TotalSeconds;
            return intResult;
        }

        /// <summary>
        /// 编辑保存上传文件的Session Key ，如果是多个文件,则session内容以|分开
        /// </summary>
        public static string EditorUpLoadFieldKey = "editor_upfile";
    }
}
